package qdu.rg012.clt_srv_msgr;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class Clt_act extends Activity {
    Intent it; Myconn mc;
    Boolean isbound;
    Messenger msgr_srv,msgr_clt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ly_clt);
        isbound = false;
        //---------准备客户端自己的消息处理 msgr对象--------
        msgr_clt = new Messenger(new Clt_Handler());

        View.OnClickListener mylis = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.btn_getsrv:
                        if(isbound && msgr_srv != null){
                            Message msg = Message.obtain();
                            msg.what = 13;
                            Bundle bd = new Bundle();
                            bd.putString("cltmsg","hello server");
                            msg.setData(bd);
                            //-------------将客户端的msgr告知服务端-------------
                            msg.replyTo = msgr_clt;

                            //-------------------------------
                            try {
                                msgr_srv.send(msg);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    case R.id.btn_c:
                        if(!isbound){
                            it = new Intent(Clt_act.this, Srv1.class);
                            mc = new Myconn();
                        }
                        isbound = bindService(it, mc, Context.BIND_AUTO_CREATE);
                        break;
                    case R.id.btn_unbind:
                        if(isbound) {
                            unbindService(mc);
                            isbound = false;
                            //mb = null;
                            msgr_srv = null;
                        }
                        break;
                }
            }
        };
        findViewById(R.id.btn_getsrv).setOnClickListener(mylis);
        findViewById(R.id.btn_c).setOnClickListener(mylis);
        findViewById(R.id.btn_unbind).setOnClickListener(mylis);
    }

    class Myconn implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            msgr_srv = new Messenger(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            msgr_srv = null;
            isbound = false;
        }
    }

    class Clt_Handler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 17:
                    Log.i("mytag", "msg from service:" + msg.getData().getString("srvmsg"));
                    break;
            }
        }
    }
}
